//package cc.md888.app.web.filter;
//
//import com.alibaba.fastjson.JSON;
//import cc.md888.app.application.user.UserService;
//import cc.md888.app.infr.bizcontext.UserInfo;
//import cc.md888.app.infr.bizcontext.impl.BizContext;
//import cc.md888.app.infr.bizcontext.impl.BizContextHolder;
//import cc.md888.app.infr.bizcontext.impl.SimpleUserInfo;
//import cc.md888.app.infr.dao.UserDao;
//import cc.md888.app.model.user.UserVO;
//import cc.md888.result.Result;
//import cc.md888.result.ResultCode;
//import org.apache.commons.collections4.CollectionUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.http.MediaType;
//import org.springframework.util.AntPathMatcher;
//import org.springframework.util.StringUtils;
//import org.springframework.web.util.UriComponents;
//import org.springframework.web.util.UriComponentsBuilder;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.PrintWriter;
//import java.util.*;
//
///**
// * Author: o_liuwei837
// * Description:
// * Date: 2024/9/18 16:46
// */
//@WebFilter(urlPatterns = "/api/*")
//public class ApiAuthorizationFilter implements Filter {
//
//    /**
//     * 忽略签名校验的路径
//     */
//    @Value("${robot.auth.ignore-path:}")
//    private Set<String> ignorePath;
//
//
//    /**
//     * apikey开关
//     */
//    @Value("${robot.auth.enable:true}")
//    private Boolean apiAuthEnable;
//
//
//    @Autowired
//    private UserService userService;
//
//
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
//        if (apiAuthEnable != Boolean.TRUE) {
//            filterChain.doFilter(request, response);
//            return;
//        }
//
//        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
//        HttpServletResponse httpServletResponse =  (HttpServletResponse) response;
//
//        if (CollectionUtils.isNotEmpty(ignorePath)) {
//            for(String pattern: ignorePath){
//                if (StringUtils.isEmpty(pattern)) {
//                    continue;
//                }
//
//                UriComponents uri = UriComponentsBuilder.fromUriString(httpServletRequest.getRequestURI()).build();
//                AntPathMatcher matcher = new AntPathMatcher();
//                if(uri.getPath() != null && matcher.match(uri.getPath(), pattern)){
//                    filterChain.doFilter(request, response);
//                    return;
//                }
//            }
//        }
//
//
//
//        String requestApikey = httpServletRequest.getHeader("X-Api-Key");
//        if (StringUtils.isEmpty(requestApikey)) {
//            writeResponse(Result.error(ResultCode.FORBIDDEN.getCode(), "no api key") , httpServletResponse);
//            return;
//        }
//
//        UserVO userVO = userService.getByApiKey(requestApikey);
//        if (userVO == null) {
//            writeResponse(Result.error(ResultCode.FORBIDDEN.getCode(), "invalid api key"), httpServletResponse);
//            return;
//        }
//
//        try{
//            SimpleUserInfo userInfo = new SimpleUserInfo(userVO.getId(), userVO.getStatus(), new HashSet<>());
//            BizContext bizContext = new BizContext(null, userInfo);
//            BizContextHolder.set(bizContext);
//            filterChain.doFilter(request, response);
//        }finally {
//            BizContextHolder.remove();
//        }
//
//    }
//
//    private void writeResponse(Object object, HttpServletResponse response) throws ServletException, IOException  {
//        String responseBody  = JSON.toJSONString(object);
//        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
//        response.setContentLength(responseBody.length());
//        PrintWriter writer = response.getWriter();
//        writer.write(responseBody);
//        writer.flush();
//    }
//
//}
